package MULE.Model.Panel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JPanel;

import MULE.Model.Logic.MapLogic;
import MULE.Model.Logic.MuleDriver;

/**
 * Main container for game components. This panel holds the map, dialogue, and
 * scoreboard panels.
 * 
 * @author jstep
 */
public class GamePanel extends JPanel {

	private MapLogic mapLogic;
	private MapPanel mapPanel;
	private GameStatusPanel statusPanel;
	private FooterPanel scoreboard;
	private JPanel mainContentArea;
	private Dimension mainContentDim;

	/**
	 * Creates a new game panel.
	 */
	public GamePanel(GameStatusPanel statusPanel, MapLogic mapLogic,
			FooterPanel scoreboard) {
		// Initialize panel.
		this.setBackground(Color.WHITE);
		this.statusPanel = statusPanel;
		this.mapLogic = mapLogic;
		this.mapPanel = (MapPanel) mapLogic.getView();
		this.scoreboard = scoreboard;
		// Use the map's dimension's as the content area size.
		this.mainContentDim = mapPanel.getPreferredSize();
		this.mainContentArea = new JPanel();
		((FlowLayout) mainContentArea.getLayout()).setVgap(0);
		mainContentArea.setPreferredSize(mainContentDim);
		mainContentArea.add(mapPanel);
	}

	/**
	 * Shows the given view in the main content area of this panel. The status
	 * and scoreboard panels are untouched.
	 * 
	 * @param view
	 *            The view to show in the main content area.
	 */
	public void setMainContent(JPanel view) {
		this.mainContentArea.removeAll();
		this.mainContentArea.add(view);
		this.validate();
		this.repaint();
	}

	/**
	 * Returns the game panel's main content area size. The main content area is
	 * between the status and scoreboard panels.
	 * 
	 * @return Dimensions of this panel's main content area.
	 */
	public Dimension getMainContentSize() {
		return this.mainContentDim;
	}

	/**
	 * Puts together this panel's components.
	 */
	public void assemble() {
		this.removeAll();
		// Set map listeners.
		if (MuleDriver.getGameLogic().getCurrentRound() > 0) {
			mapPanel.addTownListener(mapLogic);
			mapPanel.setResourceTileListener(MuleDriver.getEmplacementLogic());
			mapPanel.setKeyListener(null);
		} else {
			mapPanel.addTownListener(null);
			mapPanel.setResourceTileListener(MuleDriver
					.getPropertySelectionLogic());
			mapPanel.setKeyListener(MuleDriver.getPropertySelectionLogic());
		}
		mapPanel.setPreferredSize(mainContentDim);
		mainContentArea.add(mapPanel);
		// Assemble components.
		this.add(statusPanel);
		this.add(mainContentArea);
		this.add(scoreboard);
		this.validate();
		this.repaint();
	}

	/**
	 * Called when this panel is added to another component. Overridden to
	 * ensure that all panel components are reassembled after use by other
	 * panels.
	 */
	public void addNotify() {
		super.addNotify();
		assemble();
	}
}
